import PopupWindow from "./PopupWindow";
import {WindowContent} from "./dialog/WindowContent";

const {ccclass} = cc._decorator;
/**
 * 程序常规窗口管理层. 场景中的所有层都在此管理.
 */

@ccclass
export default class PopupWindowManager extends cc.Component {
    // zIndex 永远自增,也即待显示的window 需要修改其zIndex为最顶层.
    private _zIndex: number = 0;
    public get zIndex(): number {
        return this._zIndex;
    }

    public incZIndex(): number {
        return ++this._zIndex;
    }

    public start() {
    }

    public getView<T extends PopupWindow>(clz: { prototype: T }): T {
        const winName = clz.prototype.winName;
        if (winName) {
            let children = this.node.getComponentsInChildren(clz);
            if (children) {
                for (const comp of children) {
                    if (comp.node.name === winName) {
                        cc.log("找到同类View,", winName);
                        return comp;
                    }
                }
                return null;
            }
        }
        return this.node.getComponentInChildren(clz);
    }

    /**
     * 获取弹窗内的某个Dialog实例.
     * @param clz
     */
    public getContentView<T extends WindowContent>(clz: { prototype: T }): T {
        const winName = clz.prototype.winName;
        if (winName) {
            let children = this.node.getComponentsInChildren(clz);
            if (children) {
                for (const comp of children) {
                    if (comp.node.name === winName) {
                        cc.log("找到同类View,", winName);
                        return comp;
                    }
                }
                return null;
            }
        }
        return this.node.getComponentInChildren(clz);
    }

    public getContentViews(): WindowContent[] {
        let children = this.node.getComponentsInChildren(WindowContent);
        return children;
    }

    public findWindow<T extends WindowContent>(clz: { prototype: T }): { window: PopupWindow, content: WindowContent } {
        const winName = clz.prototype.winName;
        if (winName) {
            let children = this.node.getComponentsInChildren(PopupWindow);
            if (children) {
                for (const comp of children) {
                    let contents = comp.node.getComponentsInChildren(clz);
                    if (contents) {
                        for (const content of contents) {
                            if (content.node.name === winName) {
                                return {window: comp, content: content};
                            }
                        }
                    }
                }
                return null;
            }
        }
        return null
    }

    public addView(decor: PopupWindow) {
        let widgets = decor.getComponent(cc.Widget);
        if (!widgets) {
            widgets = decor.addComponent(cc.Widget);
        }
        this.node.addChild(decor.node, this.incZIndex());
        widgets.left = widgets.top = widgets.right = widgets.bottom = 0;
        widgets.isAlignLeft = widgets.isAlignTop = widgets.isAlignRight = widgets.isAlignBottom = true;

        widgets.updateAlignment();

        return;
    }
}
